package org.example.ui.customer;
import org.example.business.OrderBusiness;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class HistoryOrderPanel extends JPanel {
    private final JButton whole, history, cancellation;
    private static JPanel orderContainer;
    private final JScrollPane scrollPane;
    private static SpringLayout layout, orderLayout;
    public HistoryOrderPanel(){
        whole = new JButton("全部");
        history = new JButton("历史");
        cancellation = new JButton("退单");
        orderContainer = new JPanel();
        layout = new SpringLayout();
        orderLayout = new SpringLayout();
        orderContainer.setLayout(orderLayout);
        scrollPane = new JScrollPane(orderContainer);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(20);
        orderContainer.setBackground(null);
        orderContainer.setOpaque(false);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        setLayout(layout);
        orderContainer.setLayout(orderLayout);
        cancellation.setBackground(Color.white);
        whole.setBackground(Color.CYAN);
        history.setBackground(Color.white);
        OrderBusiness.orderContainer = orderContainer;
        OrderBusiness.orderLayout = orderLayout;
        OrderBusiness.updateOrders();
        add(whole);
        add(history);
        add(cancellation);
        add(scrollPane);
        setAllListener();
        layout.putConstraint(SpringLayout.NORTH, whole,50, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.WEST, whole,50, SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.EAST, whole,120, SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.SOUTH, whole,80, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.NORTH, history,50, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.WEST, history,0, SpringLayout.EAST, whole);
        layout.putConstraint(SpringLayout.EAST, history,70, SpringLayout.EAST, whole);
        layout.putConstraint(SpringLayout.SOUTH, history,80, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.NORTH, cancellation,50, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.WEST, cancellation,0, SpringLayout.EAST, history);
        layout.putConstraint(SpringLayout.EAST, cancellation,70, SpringLayout.EAST, history);
        layout.putConstraint(SpringLayout.SOUTH, cancellation,80, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.NORTH, scrollPane,50, SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.WEST, scrollPane,50, SpringLayout.EAST, cancellation);
        layout.putConstraint(SpringLayout.EAST, scrollPane,-150, SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.SOUTH, scrollPane,-50, SpringLayout.SOUTH,this);
    }
    private void setAllListener() {
        history.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cancellation.setBackground(Color.white);
                whole.setBackground(Color.white);
                history.setBackground(Color.CYAN);
                OrderBusiness.setOrders("进行中", "已取消");
            }
        });
        cancellation.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cancellation.setBackground(Color.CYAN);
                whole.setBackground(Color.white);
                history.setBackground(Color.white);
                OrderBusiness.setOrders("已完成", "进行中");
            }
        });
        whole.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cancellation.setBackground(Color.white);
                whole.setBackground(Color.CYAN);
                history.setBackground(Color.white);
                OrderBusiness.setOrders("", "");
            }
        });
    }
}
